'use strict';
exports.main = async (event, context) => {
	const db = uniCloud.database()
	const dbCmd = db.command
	const collection = db.collection('db_meizitu').orderBy('createTime','desc')
	const collection1 = db.collection('db_favorite_meizi')

	const {
		pageIndex = 1,
			pageSize = 12,
			userId=""
	} = event

	try {
		const total = (await collection.where({
			_id: dbCmd.exists(true)
		}).count()).total

		const list = (await collection.skip((pageIndex - 1) * pageSize).limit(pageSize).get()).data

		for (let i = 0, len = list.length; i < len; i++) {
			const item = list[i]
			const res = await collection1.where({
				mz_id: item._id,
				user_id: userId
			}).get()
			item.isFavorited = res.affectedDocs !== 0
		}
		return {
			code: 0,
			data: {
				list,
				total
			}
		}
	} catch (err) {
		return {
			code: -1,
			msg: "服务器错误",
		}
	}
};
